package cn.sheep.dmp.report

import java.util.Properties

import cn.sheep.dmp.utils.{CaluAdKpi, Tools}
import org.apache.spark.sql.{SQLContext, SaveMode}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * 统计广告在地域维度上的指标分布情况
  * Sheep.Old @ 64341393
  * Created 2018/3/29
  */
object Report4AreaCore {

    def main(args: Array[String]): Unit = {

        val sparkConf = new SparkConf().setAppName("地域维度指标分布")
          .setMaster("local[*]")
          .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")

        val sc = new SparkContext(sparkConf)
        val sqlc = new SQLContext(sc)

        // 读parquet文件
        val dataFrame = sqlc.read.parquet(Tools.load.getString("parquet.path"))


        dataFrame.map(row => {
            val pname = row.getAs[String]("provincename")
            val cname = row.getAs[String]("cityname")

            val rmode = row.getAs[Int]("requestmode")
            val pnode = row.getAs[Int]("processnode")
            val efft = row.getAs[Int]("iseffective")
            val bill = row.getAs[Int]("isbilling")
            val isbid = row.getAs[Int]("isbid")
            val iswin = row.getAs[Int]("iswin")
            val adoid = row.getAs[Int]("adorderid")

            val cb = row.getAs[Double]("winprice")
            val xf = row.getAs[Double]("adpayment")


            val adReq: List[Double] = CaluAdKpi.caluAdReq(rmode, pnode)
            val adRtb: List[Double] = CaluAdKpi.caluAdRbt(efft, bill, isbid, iswin, adoid)
            val adShowClick: List[Double] = CaluAdKpi.caluAdShowClick(rmode, efft)
            val adCost: List[Double] = CaluAdKpi.caluAdCost(efft, bill, iswin, cb, xf)

            //原始请求  有效请求    广告请求    参与竞价数   竞价成功数	展示量	点击量	广告成本	广告消费
            ((pname, cname), adReq ++ adRtb ++ adShowClick ++ adCost)
        }).reduceByKey((list1, list2) => {
            list1 zip list2 map(t => t._1 + t._2)
        }).map(t => {
            t._1._1 +"\t"+t._1._2 + t._2.mkString("\t")
        }).saveAsTextFile("F:\\dmp\\Rpt4Area")


        sc.stop()
    }

}
